<?php

/*
 * 个人信息控制器
 * @author turtle
 * create_time 2017-01-02
 */


namespace App\Admin\Controllers\Profile;

use App\Admin\Controllers\Controller;
use App\Admin\Models\City;
use App\Admin\Models\User;
use Illuminate\Support\Facades\Auth;

class ProfileController extends Controller
{

    /*
     * 个人信息页面
     * @author turtle
     * create_time 2017-01-02
     */

    public function index($id)
    {
        $bool = User::checkUserPermissions('check_user');
        $operator = Auth::user();
        if (!$bool) {
            return redirect()
                ->route('admin.dashboard')
                ->with([
                    'message' => "您没有拥有该权限，请联系管理员",
                    'alert-type' => 'error',
                ]);
        }
        $id = intval($id);
        $user = User::find($id);
        if (empty($user)) {
            return redirect()
                ->route('admin.dashboard')
                ->with([
                    'message' => "抱歉，该用户不存在",
                    'alert-type' => 'error',
                ]);
        } elseif ($user['city'] != $operator['city'] && $operator['id'] != 1) {
            return redirect()
                ->route('admin.users')
                ->with([
                    'message' => "抱歉，没有查看该用户的权限",
                    'alert-type' => 'error',
                ]);
        }
        $city = City::find($user['city']);
        $edit_user = User::checkUserPermissions('edit_user');
        $datas['edit_user'] = $edit_user;
        $user['address'] = $user['province'] . $city['city'];
        $datas['user'] = $user;
        return view('admin.profile', $datas);
    }

    /*
     * 编辑个人信息
     * @author turtle
     * create_time 2017-01-02
     */

    public function edit($id)
    {

        $id = intval($id);
        $user = User::find($id);

        $datas['user'] = $user;
        return view('admin.edit_profile', $datas);
    }
}
